<?php

class SkuController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
       $siteIdNamespace = new Zend_Session_Namespace('siteid');
		if (empty($siteIdNamespace->siteid)) {
			$this->_redirect("/");
		};
		$siteId = $siteIdNamespace->siteid;
		$req = $this->getRequest();
		if($req->isGet()){
			$bid = $req->get('bid');
			$this->view->bid = $bid;
			$shopId = $req ->get('shopid');
			if(!empty($shopId)){
				//rest getUserShopProductList
				$rest = new Application_Model_Rest();
				$list = $rest->getUserShopProductList($shopId);
				$arr_brand = array();
				$arr_productList = array();
				$arr_saleList = array();
				foreach($list as $row){
					if ($row->onsale == 1)
						$arr_saleList [] = $row;
					if (!array_key_exists($row->brandid,$arr_brand)) {
						$arr_brand[$row->brandid] = $row->brandname;
					}
					if($row->onsale==0)
						$arr_productList[$row->brandid][] = $row;
				}
				ksort($arr_brand);
				ksort($arr_productList);
				$this->view->arr_brand = $arr_brand;
				$this->view->arr_productList = $arr_productList;
				$this->view->arr_saleList = $arr_saleList;
				$this->view->shopid = $shopId;
				$obj = $rest->getShopDetails($shopId);
				$this->view->obj=$obj;
			} else {
				//TODO: handle empty ShopID;
				$this->_redirect('/error');
			}
		}

		if($req->isPost()){
			$auth = new Application_Model_Auth();
			if(!$auth->isLoggedIn()){
				echo '0';exit;
			}
			$rest = new Application_Model_Rest();
			$siteidNameSpace = new Zend_Session_Namespace('siteid');
			$distributerId = $siteidNameSpace->distributerId;
			//todo check distributerId is open??
			$res_open = $rest->getDistributerActive($distributerId);
			if($res_open!=true){
				echo 'distributerClosed';
				exit;
			}
			//ajax $_POST值
			$orderList = $req->getPost('list');
			$custId = $req->getPost('custid');
			$shopId = $req->getPost('shopid');
			$etime = $req->getPost('etime');
			$stime = $req->getPost('stime');
			//$acList  =  1:1,5;2,3|2:5,1;12,7
			
			$arr = explode(',',$orderList);
			$cartList = array();
			for($i=0;$i<count($arr)/4;$i++){
				$j = $i * 4;
				$cartList[] = array($arr[$j],$arr[$j+1],$arr[$j+2],
									$arr[$j+3]);
			}
			$acList = $shopId.':';
			foreach($cartList as $order) {
				$acList .= $order[1].','.$order[3].';';
			}
			$acList = rtrim($acList,';');
			/****
				*	rest postUserOrder
				*   siteid传空
			****/
			$res_postUserOrder = $rest->postUserOrder($siteId,$acList,'RShop');
			if(!empty($res_postUserOrder)){
				$oid = $res_postUserOrder[0]->id;
				echo $oid;
				exit;
			}else{
				echo 'error';
				exit;
			}
		}
    }


    public function cartAction()
    {
       /*  $siteIdNamespace = new Zend_Session_Namespace('siteid');
		$shopid = $siteIdNamespace->shopid;
		$this->view->shopid = $shopid; */
		echo json_encode(array('msg'=>'ajaxToCart'));
		exit;
    }

    public function detailsAction()
    {
       $req = $this->getRequest();
	   if($req->isGet()){
		$id = $req->get('id');
		$rest = new Application_Model_Rest();
		$res = $rest->getUserProductDetails($id);
	    if($res == false){
		   Header("Location: /index");
		   exit;
	    }
		$this->view->res = '';
		if(!empty($res)){
			$this->view->res = $res;
		}
	   }
    }
}





